//
//  SignalTableChooseView.m
//  Select
//
//  Created by dihuijun on 17/3/15.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "SignalTableChooseView.h"
#import "SignalTableViewCell.h"

#define HeaderHeight 50
#define CellHeight   50



static NSString *ID = @"signalTableCell";
@implementation SignalTableChooseView

/** 初始化创建 */
+(SignalTableChooseView *)shareTableViewWithFrame:(CGRect)frame{
    SignalTableChooseView *tablechooseView = [[SignalTableChooseView alloc]initWithFrame:frame];
    return tablechooseView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self addSubview:self.tableView];
    }
    return self;
}
/**   创建表盒   */
- (UITableView *)tableView{
    if(!_tableView){
        _tableView            = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[SignalTableViewCell class] forCellReuseIdentifier:ID];
    }
    return _tableView;
}
#pragma mark ---  <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SignalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.titleLabel.text = self.datasource[indexPath.row];
    
    return cell;
    
}
//选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ///如果 _currentSelectIndexPath有值，并且_currentSelectedIndexPath跟indexPath不相等（一开始点击_currentSelectIndexPath=nil）
    if (_currentSelectIndexPath!=nil && _currentSelectIndexPath !=indexPath) {
        SignalTableViewCell *cell = [tableView cellForRowAtIndexPath:_currentSelectIndexPath];
        
        [cell updateCellWithState:NO];
    }
    
    SignalTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell updateCellWithState:!cell.isSelected];
    _currentSelectIndexPath = indexPath;
    _block(cell.titleLabel.text,indexPath);
    
    
    
    
}
//杭高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CellHeight;
}


/** 刷新表格 */
- (void)reloadData{
    [self.tableView reloadData];
}
@end
